﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Globalization;

namespace Brain
{
    public static class RectangleUtil
    {
        public static Rectangle Scale(Rectangle rect, float scale)
        {
            return new Rectangle(
                rect.X,
                rect.Y,
                (int)(rect.Width * scale),
                (int)(rect.Height * scale));
        }
        public static Rectangle Scale(Rectangle r, Vector2 scale)
        {
            return new Rectangle(
                r.X,
                r.Y,
                (int)(r.Width * scale.X),
                (int)(r.Height * scale.Y));
        }
        public static Rectangle Lerp(Rectangle start, Rectangle end, float amount)
        {
            Rectangle novo = new Rectangle();
            novo.X = (int)(FloatUtil.Lerp(start.X, end.X, amount));
            novo.Y = (int)(FloatUtil.Lerp(start.Y, end.Y, amount));
            novo.Width = (int)(FloatUtil.Lerp(start.Width, end.Width, amount));
            novo.Height = (int)(FloatUtil.Lerp(start.Height, end.Height, amount));


            return novo;
        }
        public static Rectangle Position(Vector2 vector)
        {
            return new Rectangle((int)vector.X,
                (int)vector.Y,
                0, 0);
        }

        public static Rectangle Position(Rectangle start, Vector2 vector)
        {
            return new Rectangle((int)vector.X,
                (int)vector.Y,
                start.Width, start.Height);
        }
        public static Rectangle Position(Rectangle start, int X, int Y)
        {
            return new Rectangle(X,
                Y,
                start.Width, start.Height);
        }


        public static bool Intersects(Rectangle r1, Rectangle r2)
        {
            return !(r2.Left > r1.Right
                     || r2.Right < r1.Left
                     || r2.Top > r1.Bottom
                     || r2.Bottom < r1.Top
                    );

        }

        /// <summary>
        /// Adds Two Rectangles
        /// </summary>
        /// <param name="rect1"></param>
        /// <param name="rect2"></param>
        /// <returns></returns>
        public static Rectangle Add(Rectangle rect1,
            Rectangle rect2)
        {
            return new Rectangle(rect1.X + rect2.X,
                rect1.Y + rect2.Y,
                rect1.Width + rect2.Width,
                rect1.Height + rect2.Height);
        }

        /// <summary>
        /// Multiplies Two Rectangles
        /// </summary>
        /// <param name="rect1"></param>
        /// <param name="rect2"></param>
        /// <returns></returns>
        public static Rectangle Multiply(Rectangle rect1,
            Rectangle rect2)
        {
            return new Rectangle(rect1.X * rect2.X,
                rect1.Y * rect2.Y,
                rect1.Width * rect2.Width,
                rect1.Height * rect2.Height);
        }

        /// <summary>
        /// Multiplies A Rectangle by a float
        /// </summary>
        /// <param name="rect1"></param>
        /// <param name="rect2"></param>
        /// <returns></returns>
        public static Rectangle Multiply(Rectangle rect1,
            float value)
        {
            return new Rectangle((int)(rect1.X * value),
                (int)(rect1.Y * value),
                (int)(rect1.Width * value),
                (int)(rect1.Height * value));
        }
        /// <summary>
        /// Multiplies A Rectangle by an int
        /// </summary>
        /// <param name="rect1"></param>
        /// <param name="rect2"></param>
        /// <returns></returns>
        public static Rectangle Multiply(Rectangle rect1,
            int value)
        {
            return new Rectangle(
                rect1.X * value,
                rect1.Y * value,
                rect1.Width * value,
                rect1.Height * value);
        }

        /// <summary>
        /// Multiplies A Rectangle by floats
        /// </summary>
        /// <param name="rect1"></param>
        /// <param name="rect2"></param>
        /// <returns></returns>
        public static Rectangle Multiply(Rectangle rect1,
            float x, float y, float width, float height)
        {
            return new Rectangle((int)(rect1.X * x),
                (int)(rect1.Y * y),
                (int)(rect1.Width * width),
                (int)(rect1.Height * height));
        }

        public static Rectangle GetStringRectangle(string Text, SpriteFont font, Vector2 position)
        {
            Vector2 measure = font.MeasureString(Text);

            return new Rectangle((int)position.X, (int)position.Y, (int)measure.X, (int)measure.Y);
        }
        public static Rectangle GetStringRectangle(string Text, SpriteFont font, Vector2 position, Vector2 scale)
        {
            Vector2 measure = font.MeasureString(Text);
            measure *= scale;

            return new Rectangle((int)position.X, (int)position.Y, (int)measure.X, (int)measure.Y);
        }
        public static Rectangle GetStringRectangle(string Text, SpriteFont font, Vector2 position, float scale)
        {
            Vector2 measure = font.MeasureString(Text);
            measure *= scale;

            return new Rectangle((int)position.X, (int)position.Y, (int)measure.X, (int)measure.Y);
        }

        /// <summary>
        /// Gets the Texture's Rectangle
        /// </summary>
        /// <param name="texture"></param>
        /// <returns></returns>
        public static Rectangle GetTextureRectangle(Texture2D texture)
        {
            return new Rectangle(0, 0, texture.Width, texture.Height);
        }

        /// <summary>
        /// Gets the Texture's Rectangle
        /// </summary>
        /// <param name="texture"></param>
        /// <returns></returns>
        public static Rectangle GetTextureRectangle(Texture2D texture, Vector2 Position)
        {
            return new Rectangle((int)Position.X, (int)Position.Y, texture.Width, texture.Height);
        }

        /// <summary>
        /// Gets the Texture's Rectangle
        /// </summary>
        /// <param name="texture"></param>
        /// <returns></returns>
        public static Rectangle GetTextureRectangle(Texture2D texture, Vector2 Position, Vector2 Size)
        {
            return new Rectangle((int)Position.X, (int)Position.Y, (int)(texture.Width * Size.X), (int)(texture.Height * Size.Y));
        }

        /// <summary>
        /// Gets the Texture's Rectangle
        /// </summary>
        /// <param name="texture"></param>
        /// <returns></returns>
        public static Rectangle GetTextureRectangle(Texture2D texture, Vector2 Position, float Size)
        {
            return new Rectangle((int)Position.X, (int)Position.Y, (int)(texture.Width * Size), (int)(texture.Height * Size));
        }


        public static Rectangle FromVector2(Vector2 vec, int Width, int Height)
        {
            return new Rectangle((int)vec.X, (int)vec.Y, Width, Height);
        }


        public static int CenterRectangleX(Rectangle Rectangle, int ViewWidth)
        {
            int ViewWidthOver2 = ViewWidth / 2;
            int WidthRectangleOver2 = Rectangle.Width / 2;
            return ViewWidthOver2 - WidthRectangleOver2;
        }
        public static int CenterRectangleY(Rectangle Rectangle, int ViewHeight)
        {
            int ViewHeightOver2 = ViewHeight / 2;
            int HeightRectangleOver2 = Rectangle.Height / 2;
            return ViewHeightOver2 - HeightRectangleOver2;
        }
        public static Rectangle CenterRectangle(Rectangle Rectangle, int ViewWidth, int ViewHeight)
        {
            return new Rectangle(CenterRectangleX(Rectangle, ViewWidth),
                CenterRectangleY(Rectangle, ViewHeight), Rectangle.Width, Rectangle.Height);
        }
        public static Rectangle CenterRectangle(Rectangle Rectangle, Rectangle viewRectangle)
        {
            return new Rectangle(CenterRectangleX(Rectangle, viewRectangle.Width),
                CenterRectangleY(Rectangle, viewRectangle.Height), Rectangle.Width, Rectangle.Height);
        }

        public static Rectangle Transform(Rectangle rect, Matrix world)
        {
            Vector2 m = new Vector2(rect.X, rect.Y);
            m = Vector2.Transform(m, world);
            return new Rectangle((int)m.X, (int)m.Y, rect.Width, rect.Height);
        }



    }
}
